<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Datei: xplugin.class.php
 *
 * Class help to use all plugins
 *
 * @author Norbert Gebert
 * @copyright Norbert Gebert 02.01.2015
 * @license GPL3
 *
 * @version 0.6b
 * @package Admin
 * @subpackage global
 */
class xplugin
{

	private static $_path = './xplugin/';
	private static $_buffer = '';
	private static $_xPluginClass;
	private static $_xPluginClassName;

	/**
	 * Initializes
	 */
	function __construct()
	{
		
	}

	/**
	 * Returns an array with all registered plugins and settings
	 * @return array Associative array with plugin-data
	 */
	public static function getPluginCode($content)
	{
		db::getInstance()->where('plugin_typ', 'C');
		db::getInstance()->where('plugin_active', 1);
		$result = db::getInstance()->get('plugin');


		foreach ($result as $plugin)
		{
			$code = "{" . $plugin['plugin_code'] . ":";
			self::$_xPluginClassName = $plugin['plugin_dir'];
			self::_startClass();

			while (strpos($content, $code) >= 1)
			{
			// Extract codeblock
			$start = strpos($content, $code);
			$end = strpos($content, '}', $start);
			$replace = substr($content, $start, ($end + 1) - $start);
			$codex = self::$_xPluginClassName->getCode($replace);
			
			$content = str_replace($replace, $codex, $content);
			}
		}
		return $content;
	}

	/**
	 * Search all active xPlugins. This are Plugins who
	 * scripts must include in the <head> of a html-page, like jQuery
	 * @return string Html-code to include in <head>-area 
	 */
	public static function getHeaderScript()
	{
		// First create all entrys for head-plugins
		db::getInstance()->where('plugin_typ', 'H');
		db::getInstance()->where('plugin_active', 1);
		$result = db::getInstance()->get('plugin');
		$script = '';

		foreach ($result as $row)
		{
			$script .= $row['plugin_config'];
		}

		// then create all entrys for content-plugins
		db::getInstance()->where('plugin_typ', 'C');
		db::getInstance()->where('plugin_active', 1);
		$result = db::getInstance()->get('plugin');

		foreach ($result as $row)
		{
			self::$_xPluginClass = $row['plugin_dir'];
			self::$_xPluginClassName = $row['plugin_dir'];
			self::_startClass();
			$script .= self::$_xPluginClassName->getScripts($row['plugin_id'],
				$row['plugin_config']);
		}
		return $script;
	}

	/**
	 * Test if class loaded else load class
	 * @return boolean True if successful open/opened, otherwise false
	 */
	private static function _startClass()
	{
		$pagefile = self::$_path . self::$_xPluginClass . '/' . self::$_xPluginClass . '.xplugin.php';
		if (is_file($pagefile))
			include_once $pagefile;

		self::$_xPluginClassName = new self::$_xPluginClass;
	}

}

?>